var btnFloat =function(data,layid){
    $layid = $('#'+layid)
    var btf_right = data.float_right?data.float_right:1
    var btf_bottom = data.float_bottom?data.float_bottom:1
    btf_right=parseInt(btf_right)||0;
    btf_bottom =parseInt(btf_bottom)||0;
    if(data.float_enable=='show'){
        $layid.hide()
        var offsetright=($("body").width()-$('#scroll_container').width())/2;
        $layid.removeClass('cstlayer').appendTo('body').attr('style','')
        .css({"position":"fixed","right":(btf_right+offsetright)+"px","z-index": "999","bottom":btf_bottom+"px"}).show()
    }
}

;
function layer_media_ready_func(layerid){
    var userAgent = navigator.userAgent.toLowerCase();
	if (!/iphone/i.test(userAgent)){
		var imgover=$('#wp-media-image_'+layerid).closest('.img_over');
		imgover.children('.imgloading').width(imgover.width()).height(imgover.height());
	}
	$('#'+layerid).layer_ready(function(){
		if(typeof(layer_img_lzld)=="function"){
		layer_img_lzld(layerid);
		}
	});
}

function layer_media_lazyload_func(layerid, iswx){
    var imgwidth = $('#'+layerid+' .wp-media_content').width();
    var imgheight = $('#'+layerid+' .wp-media_content').height();	
    var imgtop = ($('#wp-media-image_'+layerid));
    if(parseInt(imgtop.css('top'))<-600&&imgtop.attr('src').indexOf('blank.gif')>0){imgtop.attr('src',imgtop.attr('data-original'));}
    if(iswx){
 	if(imgtop.offset().top<window.screen.availHeight&&imgtop.attr('src').indexOf('blank.gif')>0){
 		imgtop.attr('src',imgtop.attr('data-original'));
 	}
 	setTimeout(function(){
		 var imgdisplay = imgtop.css('display');
		 if(imgdisplay&&imgdisplay=='inline') imgtop.css('display','inline-block');
		 },1200);
    }
	// bug#4119 - 模块自适应高度导致的留白
	$('#'+layerid).bind("wrapmodheightadapt", function(){
		$('.img_over, img.paragraph_image', this).height($(this).height());
	});
};
function layer_mslider_preload_func(layerid){
    window['preload_'+layerid] = function(url, callback){
        var img = new Image();
        img.onload = function(){
        	callback(img.width, img.height);
        	img.onload = null;
        };
        img.src = url;
    };
}

function layer_mslider_ready_func(params){
    var $mscontent = $('#'+params.layerid+' > .wp-mslider_content'),
    $mswrap = $mscontent.children('ul.mslider_wrapper'),maxln = $._parseFloat(params.framecnt);
    // Images adaptive
    var width = $mscontent.width()||300,maxwidth = (maxln + 2) * width;
    var ua = navigator.userAgent.toLowerCase() || '';
    if(ua.match(/\sucbrowser\/.+\s+mobile/g)){
    	$mswrap.children('li:last').css({position: 'relative'});
    } else{
    	$mswrap.children('li:last').css({left: (0 - maxwidth)+'px',position: 'relative'});
    }
    $mswrap.css('visibility','visible');
    $mswrap.width(maxwidth).find('img').each(function(i, node){
    	var $img = $(this),imgsrc = $img.attr("data-src")||'';
    	if ($.trim(imgsrc).length == 0) return;
             window['preload_'+params.layerid](imgsrc, function(imgwidth, imgheight){
    		var tmpw = 0,tmph = 0,height = $mscontent.height();
    		$img.attr('src',imgsrc);
    		if ((imgwidth >= width) || (imgheight >= height)) {
    			var direct = (imgwidth >= width) ? (imgwidth >= imgheight) : (imgheight < imgwidth);
    			if (direct) { // Horizontal
    				$img.width(width).height("auto");tmph = $img.height();
    				$img.css("margin", ((height - tmph) / 2)+'px 0');
    			} else { // Vertical
    				$img.height(height).width("auto");tmpw = $img.width();
    				$img.css("margin", '0 '+((width - tmpw) / 2)+'px');
    			}
    		} else $img.css("margin", ((height - imgheight) / 2)+'px '+((width - imgwidth) / 2)+'px');
    		$img.css("visibility", 'visible')
    		.closest('li').css({background: 'none',width: width+'px',height: height+'px'});
    	});
    });
    // Images switch
    var duration = $._parseFloat(params.interval) * 1000;
      window['autoplay_'+params.layerid] = function(status){
    	if (params.autoplay != '1') {
    		if ($mswrap.is(':animated')) $mswrap.stop(true, false);
    		var interid = window['interid_'+params.layerid];
    		if (interid != undefined) clearInterval(interid);
    		return;
    	}
    	var $msbar = $mscontent.find('> .mslider_bar'),curindex = $msbar.children('a.local').index();
    	if (status == undefined) curindex = Math.min(curindex + 1, maxln);
    	if(window['interid_'+params.layerid]) clearInterval(window['interid_'+params.layerid]);
    	window['interid_'+params.layerid] = setInterval(function(){
    		if (curindex > maxln - 1) {
    			$mswrap.animate({left: (0 - curindex*width)+'px'}, function(){
    				$(this).css("left", '0px');
    				$msbar.children('a:eq(0)').addClass("local")
    				.siblings().removeClass("local");
                    $msbar.children('a:eq(0)').find("span").addClass("local");
                    $msbar.children('a:eq(0)').siblings().find("span").removeClass("local");
    			});
    			curindex = 0;
    		} else {
    			$mscontent.find('> .mslider_bar > a:eq('+curindex+')').triggerHandler(mclick,[curindex]);
    		}
    		curindex++;
    	}, duration);
    }
    if (window.ontouchstart !== undefined) {
    	(function(){
    		var target = $mswrap[0];var pagex = 0; var pagey = 0; var curleft = 0;
            var need_stopPropagation=false;
            if(!window['is_touch_bind_'+params.layerid]){
    		target.addEventListener("touchstart", function(e){
				var $target = $(this);
				$target.find('li.li_last').remove();
    			pagex = e.touches[0].pageX;
    			pagey = e.touches[0].pageY;
				curleft = $._parseFloat($target.css("left"));
                   need_stopPropagation=false;
    		}, false);
    		target.addEventListener("touchmove", function(e){
				if ($mswrap.is(':animated')) $mswrap.stop(true, false);
				if(curleft>=0){
					var lastli=$mswrap.find('li:last');
					var interid = window['interid_'+params.layerid];
					if (interid != undefined) clearInterval(interid);
					if(lastli.length>0){
						var cpli=lastli.clone().addClass('li_last').css('left','');
						var liw=0-width;
						$mswrap.find('li:first').before(cpli);
						$mswrap.css("left",liw+'px');
						curleft =liw;
					}
				}
    			// init
    			var movex = e.touches[0].pageX - pagex,
    			movey = e.touches[0].pageY - pagey,newleft = curleft + movex;
                   if(need_stopPropagation){
                        e.stopPropagation();
                        e.preventDefault();
                    }
    			// fixed touchmove
    			if (Math.abs(movey) < 10 && Math.abs(movex) > 5) {
    				e.preventDefault();
    				e.stopPropagation();
    				need_stopPropagation=true;
    				// animated
    				$mswrap.css("left", newleft+'px')
    				.find('li>a').bind('click',function(e){e.preventDefault()});/*Disabled <a>*/
    			}
    		}, false);
    		target.addEventListener("touchend", function(e){
    			var interid = window['interid_'+params.layerid];
    			need_stopPropagation=false;
    			if (interid != undefined) clearInterval(interid);
    			var newleft = index = 0,movex = e.changedTouches[0].pageX - pagex,moveln = 30/*moved length*/,
				curindex = $mscontent.find('> .mslider_bar > a.local').index(),moved = false;
    			if (Math.abs(movex) >= moveln) {
    				moved = true;
    				index = (movex < 0)?(curindex + 1):(curindex - 1);
    				if (index < 0) {
    					newleft = width;
    					index = maxln - 1;
    				} else {
    					newleft = 0 - index * width;
    					if((movex < 0)&&(curindex == maxln - 1)) index = 0;
    				}
    			} else {
    				index = curindex;
    				newleft = 0 - curindex * width;
				}
				if($mswrap.find('li.li_last').length){
					newleft -= width;
				}
    			$mswrap.animate({left: newleft+'px'}, function(){
					$('li>a', this).unbind('click')/*Enabled <a>*/
    				$mscontent.find('> .mslider_bar > a:eq('+index+')').addClass("local")
    				.siblings().removeClass("local");
                    $mscontent.find('> .mslider_bar > a:eq('+index+')').find("span").addClass("local");
                    $mscontent.find('> .mslider_bar > a:eq('+index+')').siblings().find("span").removeClass("local");
					if($mswrap.find('li.li_last').length){
						$mswrap.find('li.li_last').remove();
						$(this).css("left", (newleft+width)+'px');
					}
					if (moved == false){
						window['autoplay_'+params.layerid]();
						return;
					} 
    				if (index == 0) $(this).css("left", '0px');
					if (index == maxln - 1) $(this).css("left", (0 - index*width)+'px');
                    window['autoplay_'+params.layerid]();
    			});
    		}, false);
            window['is_touch_bind_'+params.layerid]=true;
            }
    	})();
    } else {
    	$mswrap.bind('mousedown.mslider', function(e){
    		e.preventDefault();
    		var $target = $(this),pagex = e.pageX,curleft = $._parseFloat($target.css("left"));
    		if ($target.is(':animated')) $target.stop(true, false);
    		var interid = window['interid_'+params.layerid];
    		if (interid != undefined) clearInterval(interid);
    		$(document).bind('mousemove.mslider', function(e){
    			e.preventDefault();
    			$target.css("left", (curleft + e.pageX - pagex)+'px')
    			.find('li>a').bind('click',function(e){e.preventDefault()});/*Disabled <a>*/
    		}).bind('mouseup.mslider', function(e){
    			e.preventDefault();
    			$(document).unbind('.mslider');
    			var newleft = index = 0,movex = e.pageX - pagex,moveln = 30/*moved length*/,
    			curindex = $mscontent.find('> .mslider_bar > a.local').index(),moved = false;
    			if (Math.abs(movex) >= moveln) {
    				moved = true;
    				index = (movex < 0)?(curindex + 1):(curindex - 1);
    				if (index < 0) {
    					newleft = width;
    					index = maxln - 1;
    				} else {
    					newleft = 0 - index * width;
    					if((movex < 0)&&(curindex == maxln - 1)) index = 0;
    				}
    			} else {
    				index = curindex;
    				newleft = 0 - curindex * width;
    			}
    			$target.animate({left: newleft+'px'}, function(){
    				$('li>a', this).unbind('click')/*Enabled <a>*/
    				$mscontent.find('> .mslider_bar > a:eq('+index+')').addClass("local")
    				.siblings().removeClass("local");
                    $mscontent.find('> .mslider_bar > a:eq('+index+')').find("span").addClass("local");
                    $mscontent.find('> .mslider_bar > a:eq('+index+')').siblings().find("span").removeClass("local");
    				if (moved == false) return;
    				if (index == 0) $(this).css("left", '0px');
    				if (index == maxln - 1) $(this).css("left", (0 - index*width)+'px');
    				window['autoplay_'+params.layerid]();
    			});
    		});
    	});
    }
    // Pager
    $mscontent.find('> .mslider_bar > a').bind(mclick, function(e, n){
    	var $target = $(this),index = n||$target.index();
    	if ($mswrap.is(':animated')) $mswrap.stop(true, false);
    	var interid = window['interid_'+params.layerid];
    	if ((n == undefined) && (interid != undefined)) clearInterval(interid);
    	$target.addClass("local").siblings().removeClass("local");
        $target.find("span").addClass("local");
        $target.siblings().find("span").removeClass("local");
    	$mswrap.animate({left: (0 - index * width)+'px'}, function(){
    		(n == undefined) && window['autoplay_'+params.layerid]();
    	});
    });
    // Autoplay
    if (params.isrefresh) {
        var interid = window['interid_'+params.layerid];
        if (interid != undefined) clearInterval(interid);
        window['autoplay_'+params.layerid]();return;
    }
    window['autoplay_'+params.layerid]('init');
}